using System;
using System.Collections.Generic;
using System.Text;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0006. Z 字形变换")]
    public class No0006_Convert : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(Convert, "LEETCODEISHIRING", 3, "LCIRETOESIIGEDHN");
            Assert.TestExe(Convert, "LEETCODEISHIRING", 4, "LDREOEIIECIHNTSG");
        }

        public string Convert(string s, int numRows)
        {
            if (numRows == 1) return s;

            List<StringBuilder> rows = new List<StringBuilder>();
            for (int i = 0; i < Math.Min(numRows, s.Length); i++)
                rows.Add(new StringBuilder());

            int curRow = 0;
            bool goingDown = false;
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                rows[curRow].Append(c);
                if (curRow == 0 || curRow == numRows - 1)
                {
                    goingDown = !goingDown;
                }
                curRow += goingDown ? 1 : -1;
            }

            StringBuilder ret = new StringBuilder();
            for (int i = 0; i < rows.Count; i++)
            {
                ret.Append(rows[i]);
            }
            return ret.ToString();
        }
    }
}
